// service/newGift/newGift.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    address: '',
    couponShow: false,
    couponList:[],
    yearList: [],
  },


  serviceIndexTap(e){
    let item = e.currentTarget.dataset.item;
    util.skip('/service/servedetail/servedetail?id='+item.id+'&seckill_id='+item.seckill_id)
  },
  getYearList(){
    http.get('goods/getlist', {
      page: 1,
      city: app.globalData.address.city,
      is_hot: 1,
    }).then(res => {
      this.setData({
        yearList: res.data
      })
    }) 
  },
  goUse(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  getCouponList(){
    http.get('coupon/getlist', {
      page: 1,
    }).then(res => {
      this.setData({
        couponList: res.data
      })
    })
  },
  getClick(e){
    let item = e.currentTarget.dataset.item;
    if(item.userState === 1) return;

    // 立即领取
    http.get('coupon/receive', {
      type: 0,
      coupon_id: item.id
    }).then(res => {
      util.toast(res.msg)
      this.setData({
        couponShow: true
      })

      this.getCouponList()
    })
  },
  onClose(){
    this.setData({
      couponShow: false
    })
  },
  // 获取热门服务数据
  getCategorygoods(){
    http.post('goods/categorygoods', {city: this.data.address.city}, false).then(res => {
      this.setData({
        goodsList: res.data[0].goodsList.concat(res.data[0].goodsList,res.data[0].goodsList)
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCouponList();
    this.getYearList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCategorygoods();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})